package com.srit.system.validator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.srit.system.bean.User;
import com.srit.system.service.UserService;

/**
 * @author chenxiling
 *登陆校验
 */
@Component
public class UserValidator implements Validator {

	@Autowired
	private UserService userService;
	@Override
	public boolean supports(Class clazz) {
		// TODO Auto-generated method stub
		return clazz.equals(User.class); 
	}

	@Override
	public void validate(Object obj, Errors errors) {
		User user=(User)obj;
		ValidationUtils.rejectIfEmpty(errors, "name", "user.name.required", "name的内容不能为空");  
        ValidationUtils.rejectIfEmpty(errors, "password", "user.password.required", "password的内容不能为空"); 
        
		String status = userService.userLogin(user);
		if("fail".equals(status)){
		errors.rejectValue("username", "usernameerror", null, "用户名不正确");
		}

		if("status".equals(status)){
		errors.rejectValue("password", "passworderror", null, "密码不正确");
		}

	}

}
